home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP2.TXT < prev    next >
Text File  |  1987-07-04  |  16KB  |  388 lines

  1.                    Chapter 2 - Getting started in Turbo C
  2.  
  3.  
  4.                             YOUR FIRST C PROGRAM
  5.  
  6.              The  best way to get started with C is to actually look
  7.         at  a  program, so load the file named  TRIVIAL.C  with  the
  8.         Integrated Environment for display on the monitor.  You  are
  9.         looking at the simplest possible C program.  There is no way
  10.         to   simplify  this  program  or  to  leave  anything   out.
  11.         Unfortunately, the program doesn't do anything.
  12.  
  13.              The  word  "main" is very important,  and  must  appear
  14.         once,  and only once in every C program.   This is the point
  15.         where execution is begun when the program is run.   We  will
  16.         see  later that this does not have to be the first statement
  17.         in  the  program  but  it must exist  as  the  entry  point.
  18.         Following  the "main" program name is a pair of  parentheses
  19.         which  are  an  indication to the compiler that  this  is  a
  20.         function.   We will cover exactly what a function is in  due
  21.         time.   For now,  I suggest that you simply include the pair
  22.         of parentheses.
  23.  
  24.              The  two curly brackets,  properly called  braces,  are
  25.         used to define the limits of the program itself.  The actual
  26.         program  statements  go between the two braces and  in  this
  27.         case,  there  are  no  statements because the  program  does
  28.         absolutely nothing.  You can compile and run this program by
  29.         hitting Alt-R if in the Integrated Environment, but since it
  30.         has no executable statements, it does nothing.  Keep in mind
  31.         however, that it is a valid C program.
  32.  
  33.                        A PROGRAM THAT DOES SOMETHING
  34.  
  35.              For  a much more interesting program,  load the program
  36.         named WRTSOME.C and display it on your monitor.   It is  the
  37.         same  as  the  previous  program  except  that  it  has  one
  38.         executable statement between the braces.
  39.  
  40.              The  executable  statement  is a  call  to  a  function
  41.         supplied as a part of your Turbo C library.  Once again,  we
  42.         will not worry about what a function is, but only how to use
  43.         this  one  named "printf".  In order to output text  to  the
  44.         monitor,  it  is  put within the  function  parentheses  and
  45.         bounded by quotation marks.  The end result is that whatever
  46.         is included between the quotation marks will be displayed on
  47.         the monitor when the program is run.
  48.  
  49.              Notice the semi-colon at the end of the line.  C uses a
  50.         semi-colon as a statement terminator,  so the semi-colon  is
  51.         required  as  a  signal to the compiler that  this  line  is
  52.         complete.   This  program  is also executable,  so  you  can
  53.         compile  and  run  it to see if it does what  you  think  it
  54.         should.
  55.  
  56.  
  57.                                  Page 7
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                    Chapter 2 - Getting started in Turbo C
  68.  
  69.  
  70.  
  71.  
  72.                       ANOTHER PROGRAM WITH MORE OUTPUT
  73.  
  74.              Load  the  program  WRTMORE.C and display  it  on  your
  75.         monitor  for an example of more output and another small but
  76.         important concept.  You will see that there are four program
  77.         statements  in  this program, each one being a call  to  the
  78.         function  "printf".   The top line will be  executed  first,
  79.         then the next, and so on, until the fourth line is complete.
  80.         The statements are executed in order from top to bottom.
  81.  
  82.              Notice  the funny character near the end of  the  first
  83.         line,  namely  the backslash.   The backslash is used in the
  84.         printf   statement  to  indicate  that  a  special   control
  85.         character  is  following.  In this case, the  "n"  indicates
  86.         that  a  "newline" is requested.  This is an  indication  to
  87.         return  the cursor to the left side of the monitor and  move
  88.         down  one  line.  It is commonly referred to as  a  carriage
  89.         return/line  feed.  Any place within text that  you  desire,
  90.         you  can put a newline character and start a new line.   You
  91.         could even put it in the middle of a word and split the word
  92.         between two lines.  The C compiler considers the combination
  93.         of the backslash and letter n as one character.
  94.  
  95.              A complete description of this program is now possible.
  96.         The  first  printf outputs a line of text  and  returns  the
  97.         carriage.   The  second printf outputs a line but  does  not
  98.         return  the carriage so that the third line is  appended  to
  99.         the second, then followed by two carriage returns, resulting
  100.         in a blank line.  Finally the fourth "printf" outputs a line
  101.         followed by a carriage return and the program is complete.
  102.  
  103.              Compile and run this program to see if it does what you
  104.         expect  it to do.   It would be a good idea at this time for
  105.         you to experiment by adding additional lines of printout  to
  106.         see if you understand how the statements really work.
  107.  
  108.                           LETS PRINT SOME NUMBERS
  109.  
  110.              Load  the  file named ONEINT.C and display  it  on  the
  111.         monitor for our first example of how to work with data in  a
  112.         C program.  The entry point "main" should be clear to you by
  113.         now as well as the beginning brace.  The first new thing  we
  114.         encounter is the line containing "int index;", which is used
  115.         to define an integer variable named "index".  The "int" is a
  116.         reserved  word  in  C, and can therefore  not  be  used  for
  117.         anything else.  It defines a variable that can have a  value
  118.         from  -32768  to  32767  in Turbo C, and  in  most  other  C
  119.         compilers  for microcomputers.  The variable name,  "index",
  120.         can be any name that follows the rules for an identifier and
  121.  
  122.  
  123.                                  Page 8
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                    Chapter 2 - Getting started in Turbo C
  134.  
  135.  
  136.         is  not one of the reserved words for Turbo C. The  Turbo  C
  137.         User's Guide has a list of reserved words on page 199.   The
  138.         final  character  on  the  line,  the  semi-colon,  is   the
  139.         statement terminator used in C.
  140.  
  141.              Note  that, even though we have defined a variable,  we
  142.         have not yet assigned a value to it.  We will see in a later
  143.         chapter  that additional integers could also be  defined  on
  144.         the  same  line,  but we will  not  complicate  the  present
  145.         situation.
  146.  
  147.              Observing the main body of the program, you will notice
  148.         that  there are three statements that assign a value to  the
  149.         variable  "index",  but only one at a time.   The first  one
  150.         assigns the value of 13 to "index", and its value is printed
  151.         out.   (We will see how shortly.)  Later, the value of 27 is
  152.         assigned to "index",  and finally 10 is assigned to it, each
  153.         value  being  printed out.   It should be intuitively  clear
  154.         that  "index"  is  indeed  a variable  and  can  store  many
  155.         different  values.   Please note that many times  the  words
  156.         "printed  out" are used to mean "displayed on the  monitor".
  157.         You  will  find that in many cases  experienced  programmers
  158.         take  this  liberty,  probably due to the "printf"  function
  159.         being used for monitor display.
  160.  
  161.                           HOW DO WE PRINT NUMBERS
  162.  
  163.              To  keep  our promise,  let's return  to  the  "printf"
  164.         statements  for a definition of how they work.   Notice that
  165.         they are all identical and that they all begin just like the
  166.         "printf"  statements  we  have  seen  before.    The   first
  167.         difference occurs when we come to the % character.   This is
  168.         a  special character that signals the output routine to stop
  169.         copying characters to the output and do something different,
  170.         namely output a variable.   The % sign is used to signal the
  171.         output  of  many different types of variables, but  we  will
  172.         restrict  ourselves  to  only one  for  this  example.   The
  173.         character  following the % sign is a "d", which signals  the
  174.         output routine to get a decimal value and output it.   Where
  175.         the decimal value comes from will be covered shortly.  After
  176.         the  "d",  we  find the familiar \n, which is  a  signal  to
  177.         return the video "carriage", and the closing quotation mark.
  178.  
  179.              All  of  the  characters between  the  quotation  marks
  180.         define  the pattern of data to be output by this  statement,
  181.         and  after  the pattern,  there is a comma followed  by  the
  182.         variable name "index".  This is where the "printf" statement
  183.         gets  the decimal value which it will output because of  the
  184.         "%d"  we saw earlier.   We could add more "%d" output  field
  185.         descriptors within the brackets and more variables following
  186.         the  description  to cause more data to be printed with  one
  187.  
  188.  
  189.                                  Page 9
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                    Chapter 2 - Getting started in Turbo C
  200.  
  201.  
  202.         statement.   Keep in mind however, that it is important that
  203.         the  number of field descriptors and the number of  variable
  204.         definitions must be the same or the runtime system will  get
  205.         confused and probably quit with a runtime error.
  206.  
  207.              Much  more  will  be  covered at a later  time  on  all
  208.         aspects  of input and output formatting.  A reasonably  good
  209.         grasp  of  these  fundamentals are  necessary  in  order  to
  210.         understand  the following lessons.  It is not  necessary  to
  211.         understand everything about output formatting at this  time,
  212.         only a fair understanding of the basics.
  213.  
  214.              Compile and run ONEINT.C and observe the output.
  215.  
  216.                         HOW DO WE ADD COMMENTS IN C
  217.  
  218.              Load the file COMMENTS.C and observe it on your monitor
  219.         for an example of how comments can be added to a C  program.
  220.         Comments  are  added to make a program more readable to  you
  221.         but the compiler must ignore the comments.   The slash  star
  222.         combination  is used in C for comment delimiters.   They are
  223.         illustrated  in the program at hand.   Please note that  the
  224.         program does not illustrate good commenting practice, but is
  225.         intended  to illustrate where comments can go in a  program.
  226.         It is a very sloppy looking program.
  227.  
  228.              The  first slash star combination introduces the  first
  229.         comment  and  the star slash at the end of  the  first  line
  230.         terminates this comment.  Note that this comment is prior to
  231.         the beginning of the program illustrating that a comment can
  232.         precede the program itself.  Good programming practice would
  233.         include  a  comment  prior  to  the  program  with  a  short
  234.         introductory  description of the program.   The next comment
  235.         is  after the "main()" program entry point and prior to  the
  236.         opening brace for the program code itself.
  237.  
  238.              The  third  comment starts after the  first  executable
  239.         statement and continues for four lines.   This is  perfectly
  240.         legal  because  a comment can continue for as many lines  as
  241.         desired  until  it is terminated.   Note carefully  that  if
  242.         anything  were included in the blank spaces to the  left  of
  243.         the  three  continuation lines of the comment,  it would  be
  244.         part  of the comment and would not be  compiled.   The  last
  245.         comment  is located following the completion of the program,
  246.         illustrating  that  comments can go nearly anywhere in  a  C
  247.         program.
  248.  
  249.              Experiment  with  this program by  adding  comments  in
  250.         other places to see what will happen. Comment out one of the
  251.         printf  statements by putting comment delimiters both before
  252.         and after it and see that it does not get printed out.
  253.  
  254.  
  255.                                  Page 10
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                    Chapter 2 - Getting started in Turbo C
  266.  
  267.  
  268.  
  269.              Comments are very important in any programming language
  270.         because  you will soon forget what you did and why  you  did
  271.         it.   It  will  be  much  easier to modify  or  fix  a  well
  272.         commented  program  a year from now than one with few or  no
  273.         comments.   You will very quickly develop your own  personal
  274.         style of commenting.
  275.  
  276.              The Turbo C compiler will allow you to "nest"  comments
  277.         which  can  be  very handy if you need to  "comment  out"  a
  278.         section  of  code  during  debugging.   In  order  to   nest
  279.         comments, you must make a change in the default by selecting
  280.         the  "Options"  menu, followed by the "Compiler"  menu,  and
  281.         finally the "Source" menu.  The default for nested  comments
  282.         is off, but changing it will not affect any of the files  in
  283.         this tutorial.
  284.  
  285.                            GOOD FORMATTING STYLE
  286.  
  287.              Load  the  file  GOODFORM.C  and  observe  it  on  your
  288.         monitor.   It  is  an example of a well  formatted  program.
  289.         Even though it is very short and therefore does very little,
  290.         it  is very easy to see at a glance what it does.   With the
  291.         experience  you have already gained in  this  tutorial,  you
  292.         should  be  able  to very quickly grasp the meaning  of  the
  293.         program in it's entirety.  Your C compiler ignores all extra
  294.         spaces  and  all carriage returns  giving  you  considerable
  295.         freedom  concerning how you format your program.   Indenting
  296.         and  adding spaces is entirely up to you and is a matter  of
  297.         personal  taste.   Compile and run the program to see if  it
  298.         does what you expect it to do.
  299.  
  300.              Now load and display the program UGLYFORM.C and observe
  301.         it.   How  long  will it take you to figure  out  what  this
  302.         program  will do?   It doesn't matter to the compiler  which
  303.         format style you use, but it will matter to you when you try
  304.         to  debug  your program.   Compile this program and run  it.
  305.         You may be surprised to find that it is the same program  as
  306.         the  last  one,  except for the formatting.   Don't get  too
  307.         worried about formatting style yet.  You will have plenty of
  308.         time  to  develop  a  style of your own  as  you  learn  the
  309.         language.   Be observant of styles as you see C programs  in
  310.         magazines, books, and other publications.
  311.  
  312.              This  should  pretty well cover the basic  concepts  of
  313.         programming  in  C,  but as there are many other  things  to
  314.         learn, we will forge ahead to additional program structure.
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                  Page 11
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                    Chapter 2 - Getting started in Turbo C
  332.  
  333.  
  334.         PROGRAMMING EXERCISES
  335.  
  336.         1. Write a program to display your name on the monitor.
  337.  
  338.         2. Modify  the  program to display your address  and  phone
  339.            number  on  separate  lines  by  adding  two  additional
  340.            "printf" statements.
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                  Page 12
  388.